package com.cnsugar.common.jdbctest.dao;

import com.cnsugar.common.jdbc.BaseJdbcDao;
import com.cnsugar.common.jdbctest.bean.SysUser;
import com.cnsugar.common.paging.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Sugar
 * @Version 2018/12/17 17:24
 */
@Component
public class SysUserDao extends BaseJdbcDao<SysUser> {

    /**
     * 根据ID删除用户
     *
     * @param id
     * @return
     */
    public int deleteById(int id) {
        return delete("id=?", new Integer[]{id});
    }

    /**
     * 根据ID查询用户
     *
     * @param id
     * @return
     */
    public SysUser findById(int id) {
        return queryForObject("id=?", new Integer[]{id});
    }

    /**
     * 分页查询
     * @param sysUser
     * @param page
     * @return
     */
    public Page find(SysUser sysUser, Page page) {
        StringBuilder wsql = new StringBuilder("1=1");
        List<Object> param = new ArrayList<>();
        if (StringUtils.isNotEmpty(sysUser.getName())) {//根据姓名查询
            wsql.append(" and ").append(SysUser.Column.NAME).append(" like ?");
            param.add("%"+sysUser.getName()+"%");
        }
        if (sysUser.getCreateDate() != null) {//查询创建时间>某个时间的数据
            wsql.append(" and ").append(SysUser.Column.CREATE_DATE).append(" > ?");
            param.add(sysUser.getCreateDate());
        }
        return queryForPage(page, wsql.toString(), param.toArray());
    }
}
